package bitch;
//The Connections handle sending and receiving Strings
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.net.SocketException;
import java.sql.Timestamp;

import constants.PROTOCOL;


public class TCPConnection extends Thread
	{
	private Socket socket;
	private Server server;
	private DataInputStream input;
	private DataOutputStream output;
	private boolean isRunning;
	private String parseString;
	private boolean buildingParseString;
	private int personID;
	public TCPConnection(Socket sock,Server net)
		{
		super();
		socket=sock;
		server=net;
		personID=-1;
		parseString="";
		buildingParseString=false;
		isRunning = true;
		try
			{
			input=new DataInputStream(socket.getInputStream());
			output=new DataOutputStream(socket.getOutputStream());
			System.out.println("Server Socket (remote port "+socket.getPort()+") connected");
			}
		catch (IOException e)
			{e.printStackTrace();}
		}
	public void sendRoom(int roomID,String roomName,int roomSize)
		{
		send("<"+PROTOCOL.ROOM+'|'+roomID+'|'+roomName+'|'+roomSize+">");
		System.out.println("Room "+roomID+" sent to "+this.personID);
		}
	public void sendDepartment(int departmentID,String departmentName)
		{
		send("<"+PROTOCOL.DEPARTMENT+'|'+departmentID+'|'+departmentName+">");
		System.out.println("Department "+departmentID+" sent to "+this.personID);
		}
	public void sendPerson(int personID,String email,String realName,int departmentID)
		{
		//Password is not broadcasted to clients.
		send("<"+PROTOCOL.PERSON+'|'+personID+'|'+email+"||"+realName+'|'+departmentID+">");
		System.out.println("Person "+personID+" sent to "+this.personID);
		}
	public void sendMessage(int type,int toPersonID,int messageID,int ownerPersonID,int appointmentID,int auxField,String optionalString,Timestamp timestamp)
		{
		send("<"+PROTOCOL.MESSAGE+'|'+type+'|'+toPersonID+'|'+messageID+'|'+ownerPersonID+'|'+appointmentID+'|'+auxField+'|'+optionalString+'|'+timestamp.getTime()+">");
		System.out.println("Message "+messageID+" sent to "+this.personID);
		}
	public void run()
		{
		while (isRunning)
			{
			try
				{
				int  i= input.read();
				if(i==-1)
					{System.out.println("The client closed the socket");
					closeSocket();
					removeSocket();}
				else
					{
					char c= (char)i;
					if(c=='<' && !buildingParseString)
						buildingParseString=true;
					else if (c=='>' && buildingParseString)
						{
						server.parse(parseString,this);
						parseString="";
						buildingParseString=false;
						}
					else if (c!='<' && c!='>' && buildingParseString)
						parseString+=c;
					else
						System.out.println("Someone is spamming the server");
					}
				}
			catch (SocketException e)
				{System.out.println("The connection died");
				closeSocket();
				removeSocket();}
			catch (IOException e)
				{e.printStackTrace();}
			}
		}
	public int getPersonID()
		{
		return personID;
		}
	public void setPersonID(int personID)
		{
		this.personID=personID;
		}
	public void send(String s)
		{
		int i=0;
		boolean fail=false;
		while(i<s.length() && !fail)
			{
			try {
				output.write((int) s.charAt(i));
				i++;
				}
			catch (SocketException e)
				{
				fail=true;
				closeSocket();
				removeSocket();
				System.out.println("The connection died");
				}
			catch (IOException e)
				{e.printStackTrace();}
			}
		}

	public void removeSocket()
		{
		System.out.println("Client ID "+personID+" removed.");
		if(server.tCPConnections!=null)
			server.tCPConnections.remove(this);
		if(personID!=-1)
			{
			server.connectedPersons.remove(personID);
			server.accesses.remove(personID);
			}
		}
	public void closeSocket()
		{
		isRunning=false;
		try
			{
			int i = socket.getPort();
			input.close();
			output.close();
			socket.close();
			System.out.println("Server Socket (remote port "+i+") closed");
			}
		catch (IOException e)
			{e.printStackTrace();}
		}
	}
